﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VTR.Title.DataAccess
{
    public class DataAccess
    {
        // Constructor
        public DataAccess()
        {

        }

        public Title TitleGet(int titleID)
        {
            try
            {
                using (VTREntities db = new VTREntities())
                {
                    return db.Titles.Include("TitleType").Where(t => t.TitleID == titleID && t.EndDate == null).FirstOrDefault();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        // Save Title Row
        public void TitleSave(Title title)
        {
            try
            {
                using (VTREntities db = new VTREntities())
                {
                    db.Titles.Add(title);
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        // Updte Title Row
        public void TitleUpdate(Title title)
        {
            try
            {
                using (VTREntities db = new VTREntities())
                {
                    //db.Titles.Attach(title);
                    db.Entry(title).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public IList<TitleType> GetTitleTypes()
        {
            try
            {
                using (VTREntities db = new VTREntities())
                {
                    return db.TitleTypes.Where(t => t.EndDate == null).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
